const { MayBe, Container } = require('./support');
const fp = require('lodash/fp');
let maybe = MayBe.of([5, 6, 1]);
//练习1：使用fp.add(x,y) fp.map(fn,x) 能让函子的值增加的函数
//value是增加的因子
let ex1 = (value) => {
    return maybe.map(arr => fp.map(i => fp.add(i, value), arr));
}

console.log(ex1(2)._value);

//练习2：能够使用fp.first 获取列表的第一个元素
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']);
let ex2 = () => {
    return xs.map(fp.first);
}
console.log(ex2()._value);

//练习3：找到user名字的首字母
let safeProp = fp.curry(function (x, o) {
    return MayBe.of(o[x]);
});
let user = { id: 2, name: 'Albert' };
let ex3 = () => {
    return safeProp('name', user)
        .map(Array.from)
        .map(fp.first);
}
console.log(ex3()._value);

//练习4: 使用MayBe重写ex4
let ex4 = function(n){
    if(n){
        return parseInt(n);
    }
}
console.log(ex4('11'));

let u_ex4 = function(n){
    return MayBe.of(n).map(parseInt);
}
console.log(u_ex4('11')._value);